package net.csdn.business.common.utils;

import org.apache.commons.lang3.StringUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName SignatureUtil
 * @Description webhook加签工具
 * @Author zhangyl
 * @Date 2023/5/7 14:04
 */
public class SignatureUtil {


    /**
     * 字节数组转16进制
     * @param bytes 需要转换的byte数组
     * @return  转换后的Hex字符串
     */
    public static String byteArrayToHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if(hex.length() < 2){
                sb.append(0);
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    /**
     * HmacSHA256 加密
     * @param message
     * @param secret
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     */
    private static byte[] computeSha256(String message, String secret)
            throws NoSuchAlgorithmException, InvalidKeyException {
        byte[] secretKey = secret.getBytes();
        SecretKeySpec signingKey = new SecretKeySpec(secretKey, "HmacSHA256");
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(signingKey);
        byte[] stringBytes = message.getBytes(Charset.forName("UTF-8"));
        byte[] hashedValue = mac.doFinal(stringBytes);
        return hashedValue;
    }


    /**
     * HmacSHA1 加密
     * @param message
     * @param secret
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     */
    private static byte[] computeSha1(String message, String secret)
            throws NoSuchAlgorithmException, InvalidKeyException {
        byte[] secretKey = secret.getBytes();
        SecretKeySpec signingKey = new SecretKeySpec(secretKey, "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        byte[] stringBytes = message.getBytes(Charset.forName("UTF-8"));
        byte[] hashedValue = mac.doFinal(stringBytes);
        return hashedValue;
    }


    /**
     * @Author zhangyl
     * @Description 生成HmacSHA1签名方法
     * @Date 11:20 2023/5/7
     * @Param
     * @return
     **/
    public static String genSha1Sign(String reqBody, String secret){
        if(StringUtils.isBlank(reqBody)||StringUtils.isBlank(secret)){
            return null;
        }
        byte[] hashedValue = null;
        try {
            hashedValue = computeSha1(reqBody,secret);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String signature =byteArrayToHexString(hashedValue);
        return "sha1=" + signature;
    }

    /**
     * @Author zhangyl
     * @Description 生成HmacSHA256签名方法
     * @Date 11:20 2023/5/7
     * @Param
     * @return
     **/
    public static String genSha256Sign(String reqBody, String secret){
        if(StringUtils.isBlank(reqBody)||StringUtils.isBlank(secret)){
            return null;
        }
        byte[] hashedValue = null;
        try {
            hashedValue = computeSha256(reqBody,secret);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String signature =byteArrayToHexString(hashedValue);
        return "sha256=" + signature;
    }

}
